const errcode = require('../utils/errcode')
const utils = require('../utils')
const sqltool = require('sqltool')

exports.createDoc = async ctx => {
  const {
    title,
    description,
    viewType
  } = ctx.request.body

  if (!title) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({ title, description, viewType }, v => typeof v !== 'undefined')
  data.userId = ctx.state.loginId

  const { insertId } = await ctx.models.doc.create(data)

  ctx.success({ id: insertId })
}

exports.updateDoc = async ctx => {
  const {
    id,
    title,
    description,
    viewType,
    status,
    sort
  } = ctx.request.body

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    description,
    viewType,
    status,
    sort
  }, v => typeof v !== 'undefined')

  if (data.title === '') return ctx.error(errcode.PARANS_NOT_VOID)

  const { affectedRows } = await ctx.models.doc.update(data, { id: id })

  ctx.success({ affectedRows })
}

exports.deleteDoc = async ctx => {
  const id = ctx.query.id

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const { affectedRows } = await ctx.models.doc.delete({ id: id })

  ctx.success({ affectedRows })
}

exports.docList = async ctx => {
  const { offset, limit } = utils.doPage(ctx)
  const where = {
    status: 1
  }

  const [list, [{ count }]] = await Promise.all([
    ctx.models.doc.findAll({
      where,
      order: {
        sort: 'desc'
      },
      offset,
      limit
    }),
    ctx.models.doc.findAll({
      attrs: [sqltool.Op.raw('count(*) as count')],
      where
    })
  ])

  ctx.success({ list, count })
}

exports.addToc = async ctx => {
  const {
    title,
    sort,
    docId,
    parentId,
    isFolder = 0
  } = ctx.request.body

  if (!title || !docId) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    sort,
    docId,
    parentId,
    isFolder
  })

  data.userId = ctx.state.loginId

  const { insertId } = await ctx.models.docToc.create(data)

  ctx.success({ id: insertId })
}

exports.updateToc = async ctx => {
  const {
    id,
    title,
    sort
  } = ctx.request.body

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const data = utils.pickBy({
    title,
    sort
  })

  const { affectedRows } = await ctx.models.docToc.update(data, { id: id })

  ctx.success({ affectedRows })
}


exports.deleteToc = async ctx => {
  const id = ctx.query.id
  const withPost = +ctx.query.with_post || 0

  if (!id) return ctx.error(errcode.REQUIRED_PARAMS)

  const { affectedRows } = await ctx.models.docToc.delete({ id: id })

  if (withPost) {
    await ctx.models.post.delete({ id: id })
  }

  ctx.success({ affectedRows })
}

exports.getDocTocList = async ctx => {
  const docId = ctx.query.docId

  if (!docId) return ctx.error(errcode.REQUIRED_PARAMS)

  const list = await ctx.models.docToc.findAll({
    aliasName: 'a',
    attrs: ['a.id', 'a.docId', 'a.sort', 'a.title', 'a.parentId', 'a.isFolder'],
    where: {
      'a.docId': docId,
      'a.status': 1
    },
    order: {
      'a.sort': 'desc',
      'a.createdAt': 'asc'
    }
  })

  ctx.success({ list })
}

exports.getDocInfo = async ctx => {
  const id = ctx.query.id

  const where = {
    id: id,
    status: 1
  }

  const doc = await ctx.models.doc.findOne({
    attrs: ['id', 'title', 'description', 'viewType', 'userId', 'createdAt', 'updatedAt'],
    where
  })

  const tocs = await ctx.models.docToc.findAll({
    aliasName: 'a',
    attrs: ['a.id', 'a.docId', 'a.sort', 'a.title', 'a.parentId', 'a.isFolder'],
    where: {
      'a.docId': id,
      'a.status': 1
    },
    order: {
      'a.sort': 'desc',
      'a.createdAt': 'asc'
    }
  })

  ctx.success({ doc, tocs })
}
